package com.ecoolex.ying.space.admin.graphql;


import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.MarktingCatalogGoods;
import com.ecoolex.ying.space.entity.OutsourcingGoodsWithBLOBs;
import com.ecoolex.ying.space.pojo.AddMarktingCatalogGoodsDTO;
import com.ecoolex.ying.space.pojo.QueryMarktingCatalogGoodsDTO;
import com.ecoolex.ying.space.ws.MarktingCatalogGoodsService;
import com.ecoolex.ying.space.ws.OutsourcingGoodsService;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "运营配置-目录管理-关联商品")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = MarktingCatalogGoodsGraphql.AUTH, name = "运营目录关联商品")
public class MarktingCatalogGoodsGraphql {

    @Reference
    private MarktingCatalogGoodsService marktingCatalogGoodsService;
    
    @Reference
    private OutsourcingGoodsService outsourcingGoodsService;

    public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-MARKETING-CATALOG-GOODS";
    
    @TypeField(type = MarktingCatalogGoods.class,field = "outsourcingGoods",description = "商品信息")
    public OutsourcingGoodsWithBLOBs outsourcingGoods(@Source MarktingCatalogGoods source) {
    	return outsourcingGoodsService.getOutsourcingGoodsById(source.getOutsourcingGoodsId());
    }

    private static final String AUTH_ADD = AUTH + "-ADD";

    @MutationField(field = "addMarktingCatalogGoods", description = "运营目录关联商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "运营目录关联商品")
    public MarktingCatalogGoods addGoods(@NonNull AddMarktingCatalogGoodsDTO add, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        return marktingCatalogGoodsService.addCatalogGoods(add, user.userName());
    }

    private static final String AUTH_DEL = AUTH + "-DEL";

    @MutationField(field = "delMarktingCatalogGoods", description = "运营目录移除关联商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "运营目录移除关联商品")
    public Boolean deleteGoods(@NonNull Integer id, AdminMemberAuthority user) {
        user.assertAuth(AUTH_DEL);
        MarktingCatalogGoods record = marktingCatalogGoodsService.getCatalogGoodsById(id);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "关联商品不存在");
        marktingCatalogGoodsService.deleteCatalogGoods(record, user.userName());
        return true;
    }
    
    private static final String AUTH_EDIT = AUTH + "-EDIT";
    
    @MutationField(field = "editMarktingCatalogGoods", description = "编辑关联商品的权重")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑关联商品的权重")
    public MarktingCatalogGoods editGoods(@NonNull Integer id,@NonNull Integer weight, AdminMemberAuthority user) {
    	user.assertAuth(AUTH_EDIT);
    	MarktingCatalogGoods record = marktingCatalogGoodsService.getCatalogGoodsById(id);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "关联商品不存在");
        record.setWeight(weight);
    	return marktingCatalogGoodsService.editGoods(record, user.userName());
    }

    private static final String AUTH_GET_RELATED_GOODS = AUTH + "-GET-RELATED-GOODS";

    @QueryField(field = "relatedMarktingCatalogGoodsList", description = "运营目录已关联商品列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET_RELATED_GOODS, name = "运营目录已关联商品列表")
    public Pagination<MarktingCatalogGoods> queryRelatedCatalogGoods(QueryMarktingCatalogGoodsDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertLogin();
        return marktingCatalogGoodsService.queryRelatedCatalogGoods(query, sorts, page);
    }

    private static final String AUTH_GET_UNRELATED_GOODS = AUTH + "-GET-UNRELATED-GOODS";

    @QueryField(field = "unrelatedMarktingCatalogGoodsList", description = "运营目录未关联商品列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET_UNRELATED_GOODS, name = "运营目录未关联商品列表")
    public Pagination<OutsourcingGoodsWithBLOBs> queryUnrelatedCatalogGoods(QueryMarktingCatalogGoodsDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertLogin();
        return marktingCatalogGoodsService.queryUnrelatedCatalogGoods(query, sorts, page);
    }
}
